import axios, { InternalAxiosRequestConfig, AxiosResponse } from "axios";
import { useUserStoreHook } from "@/store/modules/user";
import { ResultEnum } from "@/enums/ResultEnum";
import { TOKEN_KEY } from "@/enums/CacheEnum";

// 创建 axios 实例
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_API_GIS,
  timeout: 1000 * 60 * 2,
  headers: { "Content-Type": "application/json;charset=utf-8" },
});

// 请求拦截器
service.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    const accessToken = localStorage.getItem(TOKEN_KEY);
    const myrefreshToken = localStorage.getItem("myrefreshToken");
    if (accessToken) {
      config.headers.Authorization = accessToken;
      config.headers["X-Authorization"] = myrefreshToken;
    }
    return config;
  },
  (error: any) => {
    return Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  (response: AxiosResponse) => {
    // 检查配置的响应类型是否为二进制类型（'blob' 或 'arraybuffer'）, 如果是，直接返回响应对象

    const myrefreshTokenRes = response.headers["x-access-token"];
    if (myrefreshTokenRes) {
      localStorage.setItem("myrefreshToken", myrefreshTokenRes);
    }

    if (
      response.config.responseType === "blob" ||
      response.config.responseType === "arraybuffer"
    ) {
      return response;
    }
    // 响应数据为二进制流处理(Excel导出)
    if (response.data instanceof ArrayBuffer) {
      return response.data;
    }

    const { status, data, statusText } = response;
    if (status === ResultEnum.SUCCESS) {
      return data;
    }

    if (status === ResultEnum.TOKEN_INVALID) {
      ElMessageBox.confirm("当前页面已失效，请重新登录", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const userStore = useUserStoreHook();
        userStore.resetToken().then(() => {
          location.reload();
        });
      });
    } else {
      ElMessage.error(msg || "系统出错");
    }

    ElMessage.error(statusText || "系统出错");
    return Promise.reject(new Error(statusText || "Error"));
  },
  (error: any) => {
    // // 异常处理
    // if (error.response.data) {
    //   const { code, msg } = error.response.data;
    //   if (code === ResultEnum.TOKEN_INVALID) {
    ElMessageBox.confirm("当前页面已失效，请重新登录", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(() => {
      const userStore = useUserStoreHook();
      userStore.resetToken().then(() => {
        location.reload();
      });
    });
    //   } else {
    //     ElMessage.error(msg || "系统出错");
    //   }
    // }
    // return Promise.reject(error.message);
  }
);

// 导出 axios 实例
export default service;
